using Circus.CodeEditor.Serialization;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
namespace Circus.CodeEditor
{
	public class LineSeparator {
		private CodeEditor owner;
		private SeparatorOptions options;
		private Color highlightBackColor = EditConsts.DefaultLineSeparatorColor;
		private Color highlightForeColor = Color.Empty;
		private Color lineColor = EditConsts.DefaultLineSeparatorLineColor;
		private Color contentDividerColor = EditConsts.DefaultContentDividerColor;
		private int tempLine = -1;
		private int updateCount;
		protected int TempLine
		{
			get
			{
				return this.tempLine;
			}
			set
			{
				if (this.tempLine != value)
				{
					this.InvalidateLine();
					this.tempLine = value;
					this.InvalidateLine();
				}
			}
		}
	
		[DefaultValue(SeparatorOptions.None), Description("Gets or sets a set of flags customizing appearance and behaviour of the \"LineSeparator\"."), Editor("QWhale.Design.FlagEnumerationEditor, QWhale.Design", typeof(UITypeEditor))]
		public virtual SeparatorOptions Options
		{
			get
			{
				return this.options;
			}
			set
			{
				if (this.options != value)
				{
					this.options = value;
					this.OnOptionsChanged();
				}
			}
		}
		[Description("Gets or sets a background color of highlighted line.")]
		public virtual Color HighlightBackColor
		{
			get
			{
				return this.highlightBackColor;
			}
			set
			{
				if (this.highlightBackColor != value)
				{
					this.highlightBackColor = value;
					this.OnHighlightBackColorChanged();
				}
			}
		}
		[Description("Gets or sets a foreground color of highlighted line.")]
		public virtual Color HighlightForeColor
		{
			get
			{
				return this.highlightForeColor;
			}
			set
			{
				if (this.highlightForeColor != value)
				{
					this.highlightForeColor = value;
					this.OnHighlightForeColorChanged();
				}
			}
		}
		[Description("Gets or sets color of horizontal lines between particular lines in the Edit control.")]
		public virtual Color LineColor
		{
			get
			{
				return this.lineColor;
			}
			set
			{
				if (this.lineColor != value)
				{
					this.lineColor = value;
					this.OnLineColorChanged();
				}
			}
		}
		[Description("Gets or sets color of horizontal lines between particular sections(for example, methods) in the Edit control.")]
		public virtual Color ContentDividerColor
		{
			get
			{
				return this.contentDividerColor;
			}
			set
			{
				if (this.contentDividerColor != value)
				{
					this.contentDividerColor = value;
					this.OnContentDividerColorChanged();
				}
			}
		}
	
		protected virtual void OnOptionsChanged()
		{
			this.Update();
		}
		protected virtual void OnHighlightBackColorChanged()
		{
			this.Update();
		}
		protected virtual void OnHighlightForeColorChanged()
		{
			this.Update();
		}
		protected virtual void OnLineColorChanged()
		{
			this.Update();
		}
		protected virtual void OnContentDividerColorChanged()
		{
			this.Update();
		}
		protected void InvalidateLine()
		{
			if (this.owner != null && this.tempLine >= 0)
			{
				Region rectRegion = this.owner.SyntaxPaint.GetRectRegion(new Rectangle(0, this.tempLine, int.MaxValue, this.tempLine));
				if (rectRegion != null)
				{
					this.owner.Invalidate(rectRegion, false);
					rectRegion.Dispose();
				}
			}
		}
		public LineSeparator()
		{
		}
		public LineSeparator(CodeEditor owner) : this()
		{
			this.owner = owner;
		}
	
		public bool ShouldSerializeContentDividerColor()
		{
			return this.contentDividerColor != EditConsts.DefaultContentDividerColor;
		}
		public virtual int BeginUpdate()
		{
			this.updateCount++;
			return this.updateCount;
		}
		public virtual int EndUpdate()
		{
			this.updateCount--;
			if (this.updateCount == 0)
			{
				this.Update();
			}
			return this.updateCount;
		}
		
		public void Update()
		{
			if (this.updateCount == 0 && this.owner != null)
			{
				int num = this.owner.LineSpace;
				if (num != this.owner.Painter.LineSpace)
				{
					this.owner.Painter.LineSpace = num;
					this.owner.UpdateView();
				}
				else
				{
					this.owner.Invalidate();
				}
			}
		}
	
		public virtual bool NeedHighlightLine(int index)
		{
			bool result;
			if (this.owner != null && ((this.options & SeparatorOptions.HighlightCurrentLine) != SeparatorOptions.None || this.tempLine >= 0))
			{
				if (((this.tempLine >= 0) ? this.tempLine : this.owner.Position.Y) == index)
				{
					result = ((this.options & SeparatorOptions.HideHighlighting) == SeparatorOptions.None || this.owner.Focused);
					return result;
				}
			}
			result = false;
			return result;
		}
		public virtual bool NeedHighlightDisplayLine(int index)
		{
			bool result;
			if (this.owner != null && ((this.options & SeparatorOptions.HighlightCurrentLine) != SeparatorOptions.None || this.tempLine >= 0))
			{
				if (this.owner.DisplayLines.PointToDisplayPoint((this.tempLine >= 0) ? new Point(0, this.tempLine) : this.owner.Position).Y == index)
				{
					result = ((this.options & SeparatorOptions.HideHighlighting) == SeparatorOptions.None || this.owner.Focused);
					return result;
				}
			}
			result = false;
			return result;
		}
		public virtual bool NeedHighlight()
		{
			return this.owner != null && (this.options & SeparatorOptions.HighlightCurrentLine) != SeparatorOptions.None && ((this.options & SeparatorOptions.HideHighlighting) == SeparatorOptions.None || this.owner.Focused);
		}
		public virtual void TempHighlightLine(int index)
		{
			this.TempLine = index;
		}
		public virtual void TempUnhighlightLine()
		{
			this.TempLine = -1;
		}
	}
}
